<?php

namespace Modules\Buy\Http\Controllers;

use houdunwang\arr\Arr;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\View\FileViewFinder;
use Modules\Buy\Entities\Brand;
use Modules\Buy\Entities\Card;
use Modules\Buy\Entities\Catego;
use Modules\Buy\Entities\Collection;
use Modules\Buy\Entities\Foot;
use Modules\Buy\Entities\Good;
use Modules\Buy\Entities\Hotsearch;
use Modules\Buy\Entities\Huowu;
use Modules\Slider\Entities\Slider;

class HomeController extends Controller
{

    /**
     * Display a listing of the resource.
     * @return Response
     */
//    前台首页
    public function index(Catego $catego)
    {






//        dd(public_path());
        $card = Card::where('user_id',\Auth::guard('shop')->user()['id'])->get();

//        首页获得轮播图的数据
//        $slider = \DB::table('sliders')->select('thumb','url')->get();
        $slider = Slider::get();
//        dd($slider->toArray());
//        获得分级的多级目录结构
        $cate = $catego->getsoncate();
//        dump($cate);
//        dd($cate);
//        循环所有多级分类数据，往首页循环展示
        foreach ($cate as $k=>$v) {
            $cate[$k]['shop'] = [];
            $cate[$k]['lunbo'] = [];
//            dump($v);
            foreach ($v['_data'] as $kk => $vv) {
                foreach ($vv['_data'] as $kkk=>$vvv){
//                    找到三级分类当中对应的商品
//                    dump($vvv);
//                    $good = Good::where(['cate_id'=>$vvv['id'],['iskill'=>'0']])->first();
                    $good = Good::where('cate_id',$vvv['id'])->where('iskill','0')->select('id','title','pics','mallprice','goodprice')->first();
                    //向对应顶级分类中添加轮播的图片
                        $cate[$k]['lunbo'][] = [
                            'tupian' => $vvv['tupian'],
                            'desc' => $vvv['desc'],
                        ];
//                    dump($good);
                    if ($good){
                        $cate[$k]['shop'][] = $good->toArray();
                    }
                }
            }
        }
//        dd($good);
//        找到秒杀的图片在前台页面显示
        $kill = Good::where('iskill',1)->get();

//        获得今日推荐的商品，往前台循环展示
        $recommend = Good::where('iscommend',1)->limit(3)->get();
//        dd($recommend->toArray());
//        可以找到一个商品，因为我只添加了一个
//        获得促销商品的信息
        $cuxiao = Good::where([])->orderBy('mallprice','asc')->limit(3)->get();
//        dd($cuxiao->toArray());
//        $res = array_slice($cate,0,2);


        $hot = Hotsearch::where([])->select('name')->orderBy('num','desc')->limit(8)->get();





        return view('index',compact('slider','cate','kill','recommend','card','cuxiao','hot'));

    }


//    加载商品详情页面
    public function detail(Good $good,$shuxing=0)
    {



         $card = Card::where('user_id',\Auth::guard('shop')->user()['id'])->get();

//            echo 123;
//            $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();
//        js传过来的是对应商品下的属性的id，意思就是一个是商品id,一个是属性id
        if ($shuxing){
//            记住现在是在找啥，现在是在找货品。不是商品
            $huowu = Huowu::where('attr',$shuxing)->where('good_id',$good['id'])->first();
//            dump($huowu->toArray());
        }else{
            $huowu = Huowu::where('good_id',$good['id'])->first();
            $shuxing = $huowu['attr'];
        }
        $attr = $good->shopAttr($good);
//        dd(123);
//        dd($good->toArray());
//        dd($attr->toArray());
//        根据获得商品id找到对应的货品，然后获得库存

//        从足迹表里面随机找到五条数据往页面循环

        $coll = Foot::where([])->pluck('good_id');
//        dd($coll);
//        $shop = Good::whereIn('id',$coll)->get();
//        dd($shop->toArray());
        $shop = \DB::table('goods')->inRandomOrder()->take(5)->get();
//        dd($shop->toArray());

//        找到收藏表里边的记录，返回出去，进行判断
        $dushu  = Collection::where([])->select('good_id')->pluck('good_id')->toArray();

//        找到商品表的全部得到推荐商品，详情页循环
        $xiang = Good::where('iscommend',1)->limit(8)->get();
//        dd($xiang->toArray());


        return view('shop.detail',compact('good','attr','shuxing','huowu','card','shop','dushu','xiang'));

    }

//    加载商品列表页
    public function list(Catego $cid,$attrids = 0,$brandid = 0)
    {
        $card = Card::where('user_id',\Auth::guard('shop')->user()['id'])->get();
//        dd($cid->toArray());
//        路由如果和你依赖注入的模型进行约束的话就会把对应模型的数据传过来
//        dd($cid->toArray());
//        如果是三级分类就要跳到三级分类的列表

//        假设都是三级分类，我们都让他往三级分类跳
//        拿到分类数据不是目的，目的是为了拿到对应的分类商品
//        $good = Good::where('cate_id',$cid['id'])->get();
//        dd($good->toArray());
//        点击二级分类名称可以找到二级分类下的所有子分类数据
//        怎么样判断它是二级分类呢，就看是否有pid和他的id相同，如果有就代表是二级分类
//        $son找到的是二级分类下的所有子分类
//        $son = Catego::where('pid',$cid['id'])->pluck('id');
//        dd($son);
//        再根据分类id找到对应的商品
//        $goo = Good::whereIn('cate_id',$son)->get();
//        dd($goo->toArray());

//        那要是一级分类呢,先用一级分类找到二级，再用二级找三级，在用三级找商品
//        $er = Catego::where('pid',$cid['id'])->pluck('id');
//        $san = Catego::where('pid',$er)->pluck('id');
//        dd($san);
//        $go = Good::whereIn('cate_id',$san)->get();
//        dd($go->toArray());

//        获得所有的品牌数据，发送给前台
        $brand  = Brand::get();
//        dd($brand->toArray());
//        dd($good['attr']->toArray());
//        获得分类数据和属性数据，进行循环

//        根据传过来的分类id值获取他的父级分类，然后再页面展示
        $data = Catego::get()->toArray();
//        dd($data);
        $sid = $cid->toArray();
//        获得所有的父级栏目
        $zifenlei = Arr::parentChannel($data, $sid['id'], 'id','pid');
//        数组反序
        $fanxu = array_reverse($zifenlei);


//            dd(123);
            $good = $cid->sonson($cid,$attrids,$brandid);
//            dd($good);

//        获得推荐的商品
        $shop = Good::where('iscommend',1)->limit(3)->get();
//        dd($shop->toArray());


        return view('shop.list',compact('good','brand','cid','attrids','fanxu','brandid','card','shop'));

    }


//    搜索页面
    public function search(Request $request){
//        dd($request->all());
        //获取搜索的内容
        $keyword = $request->input('search');
//        dd($keyword);
        //用搜索词语找商品
        $good = Good::where('title','like','%'.$keyword.'%')->paginate(10);
        //循环搜搜到的商品
        $newArr = [];
        foreach ($good as $k => $v){
            $newArr = array_merge($newArr,explode(',',$v['attr_ids']));
        }
        $newArr = array_unique($newArr);
        $topAttrs = \Modules\Buy\Entities\Arr::whereIn('id',$newArr)->get();
        foreach ($topAttrs as $k => $v){
            //找他们的子属性
            $topAttrs[$k]['son'] = \Modules\Buy\Entities\Arr::where('pid',$v['id'])->get()->toArray();
        }
        //将搜索的词语添加到热门搜索表中
        //先判断当前被搜索的词语在热门搜索表中是否有,如果有,就找到,将查询次数加1,如果没有,才添加

        //加载搜索结果模板
//        $card = Card::where('user_id',\Auth::guard('shop')->user()->id)->get();
        if ($good->toArray()['data']){
            if ($search = Hotsearch::where('name',$keyword)->first()){
                $search->num = $search->num + 1;
                $search->save();
            }else{
                Hotsearch::create([
                    'name' => $keyword,
                    'num' => 1,
                ]);
            }
        }

        $hot = Hotsearch::where([])->select('name')->orderBy('num','desc')->limit(8)->get();

//        dd($hot->toArray());
        return view('shop.search',compact('good','card','hot','keyword'));

    }


}
